﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OperationPlugins.Applications;
using OperationPlugins.Applications.Models;
using OperationPlugins.WebApp.Applications;

namespace OperationPlugins.WebApp.ViewModes
{
    public class ApplicationsViewModel : ViewModelBase
    {
        public ApplicationsViewModel()
        {
            InstalledApplications = new List<InstalledApplication>();    
        }

        /// <summary>
        /// Current status.
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// Is the Start Application Server action enabled?
        /// </summary>
        public bool IsStartEnabled { get; set; }

        /// <summary>
        /// Is the Stop Application Server action enabled?
        /// </summary>
        public bool IsStopEnabled { get; set; }

        /// <summary>
        /// Is the Recycle Installed Application action enabled?
        /// </summary>
        public bool IsRecycleEnabled { get; set; }

        /// <summary>
        /// All installed applications.
        /// </summary>
        public List<InstalledApplication> InstalledApplications { get; set; } 


        public class InstalledApplication
        {
            /// <summary>
            /// The application id.
            /// </summary>
            public string AppId { get; set; }

            /// <summary>
            /// The application name.
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// The application version or null if not defined.
            /// </summary>
            public string Version { get; set; }

            /// <summary>
            /// Supported services.
            /// </summary>
            public string Services { get; set; }

            /// <summary>
            /// Current status.
            /// </summary>
            public string Status { get; set; }

            /// <summary>
            /// Is start application enabled?
            /// </summary>
            public bool IsStartEnabled { get; set; }

            /// <summary>
            /// Is stop application enabled?
            /// </summary>
            public bool IsStopEnabled { get; set; }

            /// <summary>
            /// Is recycle application enabled?
            /// </summary>
            public bool IsRecycleEnabled { get; set; }

            /// <summary>
            /// Is uninstall application enabled?
            /// </summary>
            public bool IsUninstallEnabled { get; set; }

            /// <summary>
            /// Is this installation broken?
            /// </summary>
            public bool IsBroken { get; set; }
        }

        /// <summary>
        /// Loads the view model.
        /// </summary>
        internal void Load()
        {
            DoAction(() =>
            {
                SetStateFields();


                var z = ApplicationManager.ApplicationDataContext.GetAppInstallations().ToArray();

            });


            // - selvitä mikä on app serverin tila (ollaanko käynnissä vai ei?)
            // - hae kaiki app-installaatiot -> jos tule virhe, niin homma seis ja virhe näytetään käyttöliittymällä!
            //      - looppaa app-installaatiot lävitse ja selvitä kunkin appin tila (ollaanko käynnissä vai ei?)
            //      - looppaa app-installaatiot lävitse ja selvitä kunkin tarkemmin tiedot
            //      - ...jos loopissa tulee virhe, niin merkitse app broken-tilaan!


            InstalledApplications.Add(new ApplicationsViewModel.InstalledApplication()
            {
                AppId = "TstApp1",
                Name = "Testi Applikaatio",
                Version = "1.0",
                Status = "Running",
                Services = "Handler, Validator" + Environment.NewLine + "<br>" + Environment.NewLine + "rivi 3",
                IsStartEnabled = false,
                IsStopEnabled = true,
                IsRecycleEnabled = true
            });

            InstalledApplications.Add(new ApplicationsViewModel.InstalledApplication()
            {
                AppId = "TstApp2",
                Name = "Testi Applikaatio",
                Version = "3.0",
                Status = "Stopped",
                Services = "Validator",
                IsStartEnabled = false,
                IsStopEnabled = false,
                IsRecycleEnabled = false,
                IsBroken = true
            });

            InstalledApplications.Add(new ApplicationsViewModel.InstalledApplication()
            {
                AppId = "TstApp2",
                Name = "Testi Applikaatio",
                Version = "2.0",
                Status = "Stopped",
                Services = "Validator",
                IsStartEnabled = true,
                IsStopEnabled = false,
                IsUninstallEnabled = true,
                IsRecycleEnabled = false
            });

            InstalledApplications.Add(new ApplicationsViewModel.InstalledApplication()
            {
                AppId = "TstApp1",
                Name = "Testi Applikaatio",
                Version = "1.0",
                Status = "Running",
                Services = "Handler, Validator" + Environment.NewLine + "<br>" + Environment.NewLine + "rivi 3",
                IsStartEnabled = false,
                IsStopEnabled = true,
                IsRecycleEnabled = true
            });

        }

        /// <summary>
        /// Starts the application server.
        /// </summary>
        internal void StartApplicationServer()
        {
            ApplicationServer.Start();
            SetStateFields();            
        }

        /// <summary>
        /// Stops the application server.
        /// </summary>
        internal void StopApplicationServer()
        {
            ApplicationServer.Stop();
            SetStateFields();
        }

        /// <summary>
        /// Recycles installed applications.
        /// </summary>
        internal void RecycleInstalledApplications()
        {
            ApplicationServer.Recycle();
            SetStateFields();            
        }

        /// <summary>
        /// Starts the application.
        /// </summary>
        /// <param name="index"><see cref="InstalledApplications"/> index.</param>
        internal void StartApplication(int index)
        {
            
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        /// <param name="index"><see cref="InstalledApplications"/> index.</param>
        internal void StopApplication(int index)
        {
            
        }

        /// <summary>
        /// Recycles the application.
        /// </summary>
        /// <param name="index"><see cref="InstalledApplications"/> index.</param>
        internal void RecycleApplication(int index)
        {            
        }

        /// <summary>
        /// Uninstalls the application.
        /// </summary>
        /// <param name="index"><see cref="InstalledApplications"/> index.</param>
        internal void UninstallApplication(int index)
        {            
        }

        private void SetStateFields()
        {
            Status = ApplicationServer.Status.ToString();
            IsStartEnabled = ApplicationServer.CanBeStarted;
            IsStopEnabled = ApplicationServer.CanBeStopped;
            IsRecycleEnabled = ApplicationServer.CanBeRecycled;
        }


    }
}